/******************************************************************************
* This file is part of is32p218 platform,
* Copyright (c) 2009 HangZhou InfoStrong CO.,LTD. All rights reserved.
* This software may only be used under the terms of a valid, current,
* end user license from InfoStrong.
* Nothing else gives you the right to use this software.   
*
* Name:   host_if.h
* Desc:   Host communication Interface header file
* Author: Jerry
* Date:   2009-10-26
* Note:   
* History:
*
******************************************************************************/
#include "def.h"
#include "isOS.h"
#include "sys_glb.h"

#ifndef __HOST_IF_H__
#define __HOST_IF_H__

#ifdef __cplusplus
extern "C" {
#endif


#define HOST_IF_CHAR_SYN		0x68	// Start Syn char
#define HOST_IF_CHAR_END		0x16	// End char

#define HOST_IF_PKG_BUFF_SIZE	(MEM_SYS_BLK_SIZE)
#define HOST_IF_TX_BUFF_SIZE	(MEM_SYS_BLK_SIZE)

typedef enum
{
	HOST_IF_MODE_NONE,			// Empty state
	HOST_IF_MODE_DLT,			// DLT package
	HOST_IF_MODE_ISDP			// InfoStrong Develop Protocol( IS-DP )
}HOST_IF_MODE;

typedef enum
{
	HOST_IF_STATE_SYN = 0,		// 68H (1B)
	HOST_IF_STATE_MAC,			// MAC address (6B)
	HOST_IF_STATE_SYN2,			// 68H (1B)
	HOST_IF_STATE_CTRL,			// Control code (1B)
	HOST_IF_STATE_LEN,			// Length of data region (1B)
	HOST_IF_STATE_DATA, 		// Data region
	HOST_IF_STATE_CHKSUM,		// CheckSum (1B), Include Syn, Mac, Syn2, Ctrl, Len and Data region
	HOST_IF_STATE_END			// 16H (1B)
}HOST_IF_STATE;
typedef enum
{
	// MSG IDLE to HOST_IF message
	MSG_IDLE_TO_HOST_IF_CHECK = 0x10,	// IDLE TO Host if 
	MSG_UART_TO_HOST_IF_DATA = 0X20,	// UART TO Host IF Data Message
	MSG_HOST_IF_END
}HOST_IF_MESSAGE;

#ifdef HOST_IF_ISDP_FLG			// IS-DP definitions

#define HOST_IF_ISDP_DWORD_SYN		0x50445349		// "ISDP"
#define HOST_IF_ISDP_CHAR_SYN		0x49	// Start Syn char
#define HOST_IF_ISDP_CHAR_SYN2		0x53	// Start Syn char
#define HOST_IF_ISDP_CHAR_SYN3		0x44	// Start Syn char
#define HOST_IF_ISDP_CHAR_SYN4		0x50	// Start Syn char
#define HOST_IF_ISDP_CHAR_END		0x0A	// End char

typedef enum
{	// "ISDP"+Ctrl(1B)+Len(1B)+Data(nB)+CS(2B)+End(1B)
	HOST_IF_ISDP_STATE_SYN = 0,		// 49H (1B)
	HOST_IF_ISDP_STATE_SYN2,		// 53H (1B)
	HOST_IF_ISDP_STATE_SYN3,		// 44H (1B)
	HOST_IF_ISDP_STATE_SYN4,		// 50H (1B)
	HOST_IF_ISDP_STATE_CTRL,		// Control code (1B)
	HOST_IF_ISDP_STATE_LEN,			// Length of data region (1B)
	HOST_IF_ISDP_STATE_DATA, 		// Data region
	HOST_IF_ISDP_STATE_CHKSUM,		// CheckSum (1B), Include Syn, Syn2, Syn3, Syn4, Ctrl, Len and Data region, CRC16
	HOST_IF_ISDP_STATE_CHKSUM2,		// 
	HOST_IF_ISDP_STATE_END			// 0AH (1B)
}HOST_IF_ISDP_STATE;

#define HOST_IF_ISDP_DATA_LEN_OFFSET	(6)		// Syn dword(4B)+ctrl(1B)+len(1B)
#define HOST_IF_ISDP_CS_LEN_OFFSET		(6)		// Syn dword(4B)+ctrl(1B)+len(1B)
#define HOST_IF_ISDP_TAIL_LEN_OFFSET	(3)		// CRC16 (check word) + End char
#define HOST_IF_ISDP_BUF_LEN_OFFSET		(HOST_IF_ISDP_CS_LEN_OFFSET+3)
#define HOST_IF_ISDP_MSG_LEN(data_len)	(data_len+HOST_IF_ISDP_BUF_LEN_OFFSET)
#define HOST_IF_ISDP_DATA_LEN	(HOST_IF_PKG_BUFF_SIZE-HOST_IF_ISDP_BUF_LEN_OFFSET)
typedef PACKED struct
{
	U32 m_syn;					// should be HOST_IF_CHAR_SYN
	U8 m_ctrl;					// Control Code
	U8 m_len;					// Length of data region
	U8 m_data[HOST_IF_ISDP_DATA_LEN];
}HostISDPLLHdr;

#endif


#define HOST_IF_TMOUT_REC_VAL	500		// 500ms
typedef enum
{
	HOST_IF_TMOUT_REC = 1		// Monitor receiving state, after syn char
}HOST_IF_TMOUT_ID;

typedef struct host_if_ctrl_tag
{
	HOST_IF_MODE m_if_mode;
	HOST_IF_STATE m_rx_state;
	U8 m_rx_len;		// Whole frame length
	U8 m_mac_len;
	U8 m_chk_sum;
	U8 m_err_id;
#ifdef HOST_IF_ISDP_FLG
	HOST_IF_ISDP_STATE m_isdp_rx_state;
#endif
	U8 *m_p_buff;
	U8 *m_p_data;
	isOS_TMR m_tmr;		
	//U8 m_tx_buff[HOST_IF_TX_BUFF_SIZE+1];					
}HostIfCtrl;

#define HOST_IF_DLT_MAC_LEN		6
#define HOST_IF_DLT_LEN_MAX		212
#define HOST_IF_REC_CS_LEN_OFFSET	(4)
#define HOST_IF_REC_DATA_LEN_OFFSET	(10)
#define HOST_IF_REC_TAIL_LEN_OFFSET	(2)
#define HOST_IF_REC_BUF_LEN_OFFSET	(4)
#define HOST_IF_REC_LEN_IDX			(10)
#define HOST_IF_SND_CS_LEN_OFFSET	(4)
#define HOST_IF_SND_DATA_LEN_OFFSET	(10)

#define HOST_IF_DLT_HEAD_OFFSET (10)
#define HOST_IF_DLT_LEN_OFFSET	(12)
#define HOST_IF_DLT_LEN(data_len)	(HOST_IF_DLT_LEN_OFFSET+data_len)
#define HOST_IF_DLT_DATA_LEN	(HOST_IF_DLT_LEN_MAX-HOST_IF_DLT_LEN_OFFSET)
typedef PACKED struct
{
	U8 m_syn;					// should be HOST_IF_CHAR_SYN
	U8 m_mac[HOST_IF_DLT_MAC_LEN];	// MAC address
	U8 m_syn2;					// should be HOST_IF_CHAR_SYN
	U8 m_ctrl;					// Control Code
	U8 m_len;					// Length of data region
	U8 m_data[HOST_IF_DLT_DATA_LEN];
}HostLLHdr;

typedef HostLLHdr HostLLRspHdr;
typedef HostLLHdr DLT645Hdr;


BOOL host_if_init( void );
void host_if_busy_set( BOOL busy_flg );
U8 host_if_cs_calc( const U8 *p_data, U32 cs_len );
BOOL host_if_send( U8 *p_snd_data, U32 snd_len );
U8 *host_if_tx_buff_get( void );

#ifdef __cplusplus
}
#endif

#endif // end __HOST_IF_H__

